﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
using qlqa.Models.LinQ;

namespace qlqa.Delegate.UserDelegate
{
    public class UserDelegate
    {
        DatabaseDataContext _database = new DatabaseDataContext();
        #region Quản lý User Type
        public IList<tb_UserType> GetDanhSachUserType(string timKiem)
        {
            var listUserType = new List<tb_UserType>();
            if (string.IsNullOrEmpty(timKiem))
            {
                listUserType = _database.tb_UserTypes.Where(t => (t.IsDelete == null || t.IsDelete == false)).ToList();
            }
            else
            {
                listUserType = _database.tb_UserTypes.Where(t => (t.IsDelete == null || t.IsDelete == false) && (t.Ten.Contains(timKiem))).ToList();
            }
            return listUserType;
        }


        public tb_UserType GetThongTinUserType(int idUserType)
        {
            return _database.tb_UserTypes.FirstOrDefault(t => t.Id == idUserType);
        }


        public bool TaoUserType(tb_UserType userType)
        {
            var res = false;
            try
            {
                _database.tb_UserTypes.InsertOnSubmit(userType);
                _database.SubmitChanges();
                res = true;
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }


        public bool CapNhatUserType(tb_UserType userType)
        {
            var res = false;
            var userTypeOld = _database.tb_UserTypes.FirstOrDefault(t => t.Id == userType.Id);
            try
            {
                if (userTypeOld != null)
                {
                    userTypeOld.Ten = userType.Ten;
                    userTypeOld.IsSuDung = userType.IsSuDung;
                    _database.SubmitChanges();
                    res = true;
                }
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }

        public bool XoaUserType(int userTypeId)
        {
            var res = false;
            var userTypeOod = _database.tb_UserTypes.FirstOrDefault(t => t.Id == userTypeId);
            try
            {
                if (userTypeOod != null)
                {
                    userTypeOod.IsDelete = true;
                    _database.SubmitChanges();
                    res = true;
                }
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }

        public bool TrangThaiSuDung(bool trangThai, int userTypeId)
        {
            var res = false;
            var userTypeOod = _database.tb_UserTypes.FirstOrDefault(t => t.Id == userTypeId);
            try
            {
                if (userTypeOod != null)
                {
                    userTypeOod.IsSuDung = trangThai;
                    _database.SubmitChanges();
                    res = true;
                }
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }


        public SelectList BuildTaiKhoanSelectList(int? selectedvalue)
        {
            //IEnumerable<tb_UserType> list = _database.tb_UserTypes.Where(l=>l.IsSuDung==true && (l.IsDelete== false || l.IsDelete==null)).ToList();

            IList<object> selectList = new List<object> { new { Value = 0, Text = "-- Chọn loại tài khoản --" } };

            //foreach (var item in list)
            //{
                selectList.Add(new { Value = 1, Text = "Quản lý"});
                selectList.Add(new { Value = 2, Text = "Nhân viên" });
            //}

            return new SelectList(selectList, "Value", "Text", selectedvalue);
        }
        #endregion
        #region Quản lý User
        public IList<tb_User> GetDanhSachUser(string timKiem)
        {
            var listUser = new List<tb_User>();
            if (string.IsNullOrEmpty(timKiem))
            {
                listUser = _database.tb_Users.Where(t => (t.IsDelete == null || t.IsDelete == false)).ToList();
            }
            else
            {
                listUser = _database.tb_Users.Where(t => (t.IsDelete == null || t.IsDelete == false) && (t.Ten.Contains(timKiem))).ToList();
            }
            return listUser;
        }


        public tb_User GetThongTinUser(int idUser)
        {
            return _database.tb_Users.FirstOrDefault(t => t.Id == idUser);
        }


        public bool TaoUserType(tb_User user)
        {
            var res = false;
            try
            {
                user.Password = GetMd5Hash(user.Password);
                _database.tb_Users.InsertOnSubmit(user);
                _database.SubmitChanges();
                res = true;
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }


        public bool CapNhatUser(tb_User user)
        {
            var res = false;
            var userOld = _database.tb_Users.FirstOrDefault(t => t.Id == user.Id);
            try
            {
                if (userOld != null)
                {
                    if (!string.IsNullOrEmpty(user.Password))
                    {
                        userOld.Password = GetMd5Hash(user.Password);
                    }
                    userOld.Ten = user.Ten;
                    userOld.UserType = user.UserType;
                    userOld.IsSuDung = user.IsSuDung;
                    _database.SubmitChanges();
                    res = true;
                }
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }

        public bool XoaUser(int userId)
        {
            var res = false;
            var userOod = _database.tb_Users.FirstOrDefault(t => t.Id == userId);
            try
            {
                if (userOod != null)
                {
                    userOod.IsDelete = true;
                    _database.SubmitChanges();
                    res = true;
                }
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }

        public bool TrangThaiSuDungUser(bool trangThai, int userId)
        {
            var res = false;
            var userOod = _database.tb_Users.FirstOrDefault(t => t.Id == userId);
            try
            {
                if (userOod != null)
                {
                    userOod.IsSuDung = trangThai;
                    _database.SubmitChanges();
                    res = true;
                }
            }
            catch (Exception)
            {

                res = false;
            }
            return res;
        }

        public static string GetMd5Hash(string value)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(value));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
        #endregion
    }
}